#include "measureresultpage.h"
#include "ui_measureresultpage.h"
#include "DBOperation/mysql.h"
#include "Config/constant.h"
#include "qdebug.h"
#include "HRVPro/hrvdataprocess.h"
#include "Report/mentalreport.h"
#include<QtPrintSupport/QPrinter>
#include "Report/printreport.h"
#include "Report/login.h"
#include "Report/anf.h"
#include "Report/hrv.h"
#include "Report/appgreport.h"
#include "dialog/myhelper.h"

MeasureResultPage::MeasureResultPage(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::MeasureResultPage)
{
    ui->setupUi(this);
    if(Constant::ShowFullScreen)
        this->showFullScreen();
    setBg();
    ui->tabWidget->setCurrentIndex(0);
    ui->progressBar_psi->setTextVisible(false);
    ui->progressBar_mental->setTextVisible(false);
    ui->progressBar_Activity->setTextVisible(false);
    ui->progressBar_Ballance->setTextVisible(false);
    ui->progressBar_Emotion->setTextVisible(false);
    ui->progressBar_Fatigure->setTextVisible(false);
    ui->progressBar_Heart->setTextVisible(false);
    ui->progressBar_RegistPressure->setTextVisible(false);
    ui->progressBar_BVT->setTextVisible(false);
    ui->progressBar_DPI->setTextVisible(false);
    ui->progressBar_RBV->setTextVisible(false);
    ui->progressBar_SP->setTextVisible(false);
    ui->checkBox11->setEnabled(false);
    ui->checkBox12->setEnabled(false);
    ui->checkBox13->setEnabled(false);
    ui->checkBox14->setEnabled(false);
    ui->checkBox15->setEnabled(false);
    ui->checkBox16->setEnabled(false);
    QChart *chart=mrpc.CreateLineChart_HRV();
    ui->widget_HRV->setChart(chart);
    ui->widget_HRV->setRenderHint(QPainter::Antialiasing, true);//设置无锯齿

    QChart *chart_psd=mrpc.CreateLineChart_PSD();
    ui->widget_2->setChart(chart_psd);
    ui->widget_2->setRenderHint(QPainter::Antialiasing, true);//设置无锯齿

    QChart *chart_ppg=mrpc.CreateLineChart_PPG();
    ui->widget_PresentPPG->setChart(chart_ppg);
    ui->widget_PresentPPG->setRenderHint(QPainter::Antialiasing,true);

    QChart *chart_appg=mrpc.CreateLineChart_aPPG();
    ui->widget_PresentAPPG->setChart(chart_appg);
    ui->widget_PresentAPPG->setRenderHint(QPainter::Antialiasing,true);

    QSettings *setting=new QSettings("./Configuration.ini",QSettings::IniFormat);
    setting->beginGroup("Config");
    printType=setting->value("PrintType").toString();
    setting->endGroup();
    delete setting;
}

MeasureResultPage::~MeasureResultPage()
{
    delete ui;
}
/**
 * 功能：设置背景图片
 * @brief MeasureResultPage::setBg
 */
void MeasureResultPage::setBg()
{
    QPalette pal = this->palette();
    pal.setBrush(QPalette::Window,QBrush(QPixmap(":/images/MeasurePage/measure_bg.png")));
    setPalette(pal);
}

void MeasureResultPage::on_bt_Exit_clicked()
{
    this->close();
}

void MeasureResultPage::getUserInfoFromMeasure(UserInfoBeab uib)
{
    ui->label_ID_V_->setText(uib.UserID);
    ui->label_Age_V_->setText(uib.UserAge);
    ui->label_Height_V_->setText(uib.UserHeight);
    ui->label_Weight_V_->setText(uib.UserWeight);
    ui->label_Name_V_->setText(uib.UserName);
    ui->label_Sex_V_->setText(uib.UserSex);
    QString MeasureTime=uib.MeasureTime;
    QString file=Constant::DataBasePath;
    MySql *mySql = new MySql(file);
    dbs=mySql->DataQueryByMeasureTime(MeasureTime);
    delete mySql;
    qDebug()<<dbs.UserID;
    qDebug()<<dbs.MeasureTime;
    qDebug()<<dbs.hrvIndexResults;
    HRVDataProcess hrvdataPro;
    HRVIndex hi=hrvdataPro.Json2HRVData(dbs.hrvIndexResults);
    ui->label_AvarageHR_V->setText(QString::number(hi.sl.MeanHrt));
    ui->label_MaxHR_V->setText(QString::number(hi.sl.MaxHrt));
    ui->label_MinHR_V->setText(QString::number(hi.sl.MinHrt));
    ui->label_AbnormalBeat_V->setText(QString::number(hi.sl.AbnormalHeartNum));
    ui->label_Average_Ox_V->setText(QString::number(hi.sl.ox));
    ui->label_Perfustion_V->setText(QString::number(hi.sl.Perfusion,'f',2));

    ui->label_HRV_RRMean_V->setText(QString::number(hi.time.RR_mean,'f',1));
    ui->label_HRV_SDNN_V->setText(QString::number(hi.time.RR_SDNN,'f',1));
    ui->label_HRV_RMSSD_V->setText(QString::number(hi.time.RR_RMSSD,'f',1));
    ui->label_HRV_ApEN_V->setText(QString::number(hi.time.ApEn,'f',2));


    ui->label_HRV_TP_V->setText(QString::number(hi.freq.TP,'f',1));
    ui->label_HRV_VLF_V->setText(QString::number(hi.freq.VLF,'f',1));
    ui->label_HRV_LF_V->setText(QString::number(hi.freq.LF,'f',1));
    ui->label_HRV_HF_V->setText(QString::number(hi.freq.HF,'f',1));
    ui->label_HRV_LFHF_V->setText(QString::number(hi.freq.LFHF,'f',1));

//    QChart *chart=mrpc.CreateLineChart_HRV();
//    ui->widget_HRV->setChart(chart);
//    ui->widget_HRV->setRenderHint(QPainter::Antialiasing, true);//设置无锯齿
    mrpc.setHRVData(hi.time.RR);

//    QChart *chart_psd=mrpc.CreateLineChart_PSD();
//    ui->widget_2->setChart(chart_psd);
//    ui->widget_2->setRenderHint(QPainter::Antialiasing, true);//设置无锯齿
    mrpc.setPSDData(hi.freq.freq,hi.freq.psd);


//    QChart *chart_ppg=mrpc.CreateLineChart_PPG();
//    ui->widget_PresentPPG->setChart(chart_ppg);
//    ui->widget_PresentPPG->setRenderHint(QPainter::Antialiasing,true);
    mrpc.setPPGData(hi.appg.Pre_PPG);

//    QChart *chart_appg=mrpc.CreateLineChart_aPPG();
//    ui->widget_PresentAPPG->setChart(chart_appg);
//    ui->widget_PresentAPPG->setRenderHint(QPainter::Antialiasing,true);
    mrpc.setaPPGData(hi.appg.Pre_APPG);

    qDebug()<<"PsiScore="<<hi.mp.PsiScore;
    qDebug()<<"MsiScore="<<hi.mp.MsiScore;
    qDebug()<<"tpScore="<<hi.mp.tpScore;
    qDebug()<<"AnsScore="<<hi.mp.AnsScore;
    qDebug()<<"hfScore="<<hi.mp.hfScore;
    qDebug()<<"lfScore="<<hi.mp.lfScore;
    qDebug()<<"SdnnScore="<<hi.mp.SdnnScore;
    qDebug()<<"TotalScore="<<hi.mp.TotalScore;

    ui->progressBar_psi->setValue(hi.mp.PsiScore);//身体压力
    ui->progressBar_mental->setValue(hi.mp.MsiScore);//精神压力
    ui->progressBar_Activity->setValue(hi.mp.tpScore);//活性
    ui->progressBar_Ballance->setValue(hi.mp.AnsScore);//平衡性

    ui->label_psi->setText(QString::number(hi.mp.PsiScore,'f',1));
    ui->label_mp->setText(QString::number(hi.mp.MsiScore,'f',1));
    ui->label_activity->setText(QString::number(hi.mp.tpScore,'f',1));
    ui->label_ballance->setText(QString::number(hi.mp.AnsScore,'f',1));
    qDebug()<<"平衡得分="<<hi.mp.AnsScore;
    qDebug()<<"平衡状态="<<hi.mp.Ans_State;
    ui->progressBar_Emotion->setValue(hi.mp.EmotionScore);//情绪状态
    ui->progressBar_Fatigure->setValue(hi.mp.lfScore);//疲劳
    ui->progressBar_Heart->setValue(hi.mp.hfScore);//心脏稳定性
    ui->progressBar_RegistPressure->setValue(hi.mp.SdnnScore);//抗压能力

    ui->label_emotion->setText(QString::number(hi.mp.EmotionScore,'f',1));
    ui->label_fatigure->setText(QString::number(hi.mp.lfScore,'f',1));
    ui->label_heart->setText(QString::number(hi.mp.hfScore,'f',1));
    ui->label_ky->setText(QString::number(hi.mp.SdnnScore,'f',1));

    ui->label_PressureScore_V->setText(QString::number(hi.mp.TotalScore,'f',1));

    ui->textEdit_SL_Res_1->setText(hi.SLResults);
    ui->textEdit_MP_Res->setText(hi.MentalPressureResults);
    ui->textEdit_Ans_Res->setText(hi.AnsResults);
    QList<double>range_DPI,range_SP,range_BVT,range_RBV;
    range_DPI.append(0);
    range_DPI.append(-20);
    range_DPI.append(-120);
    range_DPI.append(-150);

    range_SP.append(0);
    range_SP.append(-20);
    range_SP.append(-50);
    range_SP.append(-150);

    range_BVT.append(0);
    range_BVT.append(-2);
    range_BVT.append(-13);
    range_BVT.append(-50);

    range_RBV.append(-50);
    range_RBV.append(-30);
    range_RBV.append(-5);
    range_RBV.append(0);
    ui->progressBar_DPI->setValue(getIndexLen(range_DPI,hi.appg.DPI,false));
    ui->progressBar_SP->setValue(getIndexLen(range_SP,hi.appg.SP,false));
    if(hi.appg.BVT>0){
        ui->progressBar_BVT->setValue(getIndexLen(range_BVT,-1*hi.appg.BVT,false));
        qDebug()<<"血管弹性-1"<<getIndexLen(range_DPI,-1*hi.appg.BVT,false);
    }
    else{
        ui->progressBar_BVT->setValue(getIndexLen(range_BVT,hi.appg.BVT,false));
        qDebug()<<"血管弹性"<<getIndexLen(range_DPI,hi.appg.BVT,false);
    }

    ui->progressBar_RBV->setValue(getIndexLen(range_RBV,hi.appg.RBV,true));
    ui->label_DPIV->setText(QString::number(hi.appg.DPI,'f',2));
    ui->label_SPV->setText(QString::number(hi.appg.SP,'f',2));
    ui->label_BVTV->setText(QString::number(hi.appg.BVT,'f',2));
    ui->label_RBVV->setText(QString::number(hi.appg.RBV,'f',2));
    ui->label_k->setText(QString::number(hi.appg.K,'f',2));
    ui->label_vascularage->setText(QString::number(hi.appg.Age));
    ui->checkBox11->setChecked(false);
    ui->checkBox12->setChecked(false);
    ui->checkBox13->setChecked(false);
    ui->checkBox14->setChecked(false);
    ui->checkBox15->setChecked(false);
    ui->checkBox16->setChecked(false);
    if(hi.appg.VascularState==0)
    {
        ui->checkBox11->setChecked(true);
    }else if(hi.appg.VascularState==1)
    {
        ui->checkBox12->setChecked(true);
    }else if(hi.appg.VascularState==2)
    {
        ui->checkBox13->setChecked(true);
    }else if(hi.appg.VascularState==3)
    {
        ui->checkBox14->setChecked(true);
    }else if(hi.appg.VascularState==4)
    {
        ui->checkBox15->setChecked(true);
    }else if(hi.appg.VascularState==5)
    {
        ui->checkBox16->setChecked(true);
    }

    ui->textEdit_Vascular_Res->setText(hi.appg.Vascular_res);


}

int MeasureResultPage::getIndexLen(QList<double>ranger, double indexv,bool flag){
    //flag=true,范围从小变大，否则是从大变小
    double a=ranger.at(0);
    double b=ranger.at(1);
    double c=ranger.at(2);
    double d=ranger.at(3);
    int len=0;
    if(flag){
        if(indexv<a)
            indexv=a;
        if(indexv>d)
            indexv=d;
        if(indexv>=a && indexv<b){
            len=150-150*(b-indexv)/(b-a);
        }else if(indexv>=b && indexv<c){
            len=300-150*(c-indexv)/(c-b);
        }else{
            len=450-150*(d-indexv)/(d-c);
        }

    }else{
        if(indexv>a)
            indexv=a;
        if(indexv<d)
            indexv=d;
        if(indexv<=a && indexv>b){
            len=150-150*(b-indexv)/(b-a);
        }else if(indexv<=b && indexv>c){
            len=300-150*(c-indexv)/(c-b);
        }else{
            len=450-150*(d-indexv)/(d-c);
        }
    }

    return len;

}
void MeasureResultPage::on_pushButton_SL_clicked()
{
    ui->tabWidget->setCurrentIndex(0);

}


void MeasureResultPage::on_pushButton_HRV_clicked()
{
    ui->tabWidget->setCurrentIndex(1);
}


void MeasureResultPage::on_pushButton_MentalPressure_clicked()
{
    ui->tabWidget->setCurrentIndex(2);
}


void MeasureResultPage::on_pushButton_Ans_clicked()
{
    ui->tabWidget->setCurrentIndex(3);
}


void MeasureResultPage::on_bt_Print_clicked()
{
    qDebug()<<"on_bt_Print_clicked";
    if(Constant::ReportType==2)
    {
        qDebug()<<"on_bt_Print_clicked"<<"ReportType==2";
        PrintReport pr;
        pr.Report_HRV_Professional(dbs);
        pr.Report_APPG_Professional(dbs);
    }else if(Constant::ReportType==1){
        qDebug()<<"on_bt_Print_clicked"<<"ReportType==1";
        MentalReport w;
        w.setV(dbs);
        QPrinter printer;
//        if(!Constant::PrintType){
//            QString ParentFolder="D:\\data\\"+dbs.UserID;
//            QDir dir(ParentFolder);
//            QString fileName;
//            if(dir.exists())
//            {
//                fileName=ParentFolder+"\\"+dbs.MeasureTime+".pdf";
//            }else
//            {
//                bool ok = dir.mkpath(ParentFolder);//创建多级目录
//                if(ok)
//                {
//                    fileName=ParentFolder+"\\"+dbs.MeasureTime+".pdf";
//                }
//            }
//            printer.setOutputFileName(fileName);
//        }
        printer.setResolution(300);//值越大分辨率越高
        printer.setPageMargins(QMarginsF(0, 0, 0, 0));

        QPixmap pixmap=w.grab(w.rect()); //抓取界面widget区域，可以抓取任意控件区域
        QPainter painter;
        painter.begin(&printer);
        painter.drawPixmap(0,0,pixmap); //按照坐标画图
        printer.setPageSize(QPageSize::A4);
    }else if(Constant::ReportType==3)
    {
        qDebug()<<"on_bt_Print_clicked"<<"ReportType==3";

        QPrinter printer,printer2;
        printer.setPageSize(QPageSize::A4);
        printer2.setPageSize(QPageSize::A4);
        //输出文件到d盘data文件夹
        qDebug()<<printType;
        if(printType=="输出pdf"){
            qDebug()<<"on_bt_Print_clicked"<<"输出pdf";
            QString ParentFolder="D:\\data\\"+dbs.UserID;
            QDir dir(ParentFolder);
            QString fileName;
            if(dir.exists())
            {
                fileName=ParentFolder+"\\"+dbs.MeasureTime.replace(":","")+".pdf";
            }else
            {
                bool ok = dir.mkpath(ParentFolder);//创建多级目录
                if(ok)
                {
                    fileName=ParentFolder+"\\"+dbs.MeasureTime.replace(":","")+".pdf";
                }
            }
            printer.setOutputFileName(fileName);

            printer.setResolution(300);//值越大分辨率越高
            printer.setPageMargins(QMarginsF(0, 0, 0, 0));
            Login report1;
            HRV report2;
            Anf report3;
            APPGReport report4;
            report1.setValue(dbs);
            report2.setValue(dbs);
            report3.setValue(dbs);
            report4.setValue(dbs);

            QPixmap pixmap1=report1.grab(report1.rect()); //抓取界面widget区域，可以抓取任意控件区域
            QPixmap pixmap2=report2.grab(report2.rect()); //抓取界面widget区域，可以抓取任意控件区域
            QPixmap pixmap3=report3.grab(report3.rect()); //抓取界面widget区域，可以抓取任意控件区域
            QPixmap pixmap4=report4.grab(report4.rect()); //抓取界面widget区域，可以抓取任意控件区域
            QPainter painter;
            painter.begin(&printer);
            painter.drawPixmap(0,0,pixmap1); //按照坐标画图
            printer.newPage();
            painter.drawPixmap(0,0,pixmap2); //按照坐标画图
            printer.newPage();
            painter.drawPixmap(0,0,pixmap3); //按照坐标画图
            printer.newPage();
            painter.drawPixmap(0,0,pixmap4); //按照坐标画图
            painter.end();
            myHelper::ShowMessageBoxInfo("导出完毕！");

        }else if(printType=="打印机打印"){
            qDebug()<<"打印机打印";
            qDebug()<<"on_bt_Print_clicked"<<"打印机打印";
            printer.setResolution(300);//值越大分辨率越高
            printer.setPageMargins(QMarginsF(0, 0, 0, 0));
            Login report1;
            HRV report2;
            Anf report3;
            APPGReport report4;
            report1.setValue(dbs);
            report2.setValue(dbs);
            report3.setValue(dbs);
            report4.setValue(dbs);

            QPixmap pixmap1=report1.grab(report1.rect()); //抓取界面widget区域，可以抓取任意控件区域
            QPixmap pixmap2=report2.grab(report2.rect()); //抓取界面widget区域，可以抓取任意控件区域
            QPixmap pixmap3=report3.grab(report3.rect()); //抓取界面widget区域，可以抓取任意控件区域
            QPixmap pixmap4=report4.grab(report4.rect()); //抓取界面widget区域，可以抓取任意控件区域
            QPainter painter;
            painter.begin(&printer);
            painter.drawPixmap(0,0,pixmap1); //按照坐标画图
            printer.newPage();
            painter.drawPixmap(0,0,pixmap2); //按照坐标画图
            printer.newPage();
            painter.drawPixmap(0,0,pixmap3); //按照坐标画图
            printer.newPage();
            painter.drawPixmap(0,0,pixmap4); //按照坐标画图
            painter.end();
        }else if(printType=="输出pdf并打印"){

            qDebug()<<"on_bt_Print_clicked"<<"输出pdf并打印";
            QString ParentFolder="D:\\data\\"+dbs.UserID;
            QDir dir(ParentFolder);
            QString fileName;
            if(dir.exists())
            {
                fileName=ParentFolder+"\\"+dbs.MeasureTime.replace(":","")+".pdf";
            }else
            {
                bool ok = dir.mkpath(ParentFolder);//创建多级目录
                if(ok)
                {
                    fileName=ParentFolder+"\\"+dbs.MeasureTime.replace(":","")+".pdf";
                }
            }
            printer.setOutputFileName(fileName);

            printer.setResolution(300);//值越大分辨率越高
            printer.setPageMargins(QMarginsF(0, 0, 0, 0));

            printer2.setResolution(300);//值越大分辨率越高
            printer2.setPageMargins(QMarginsF(0, 0, 0, 0));
            Login report1;
            HRV report2;
            Anf report3;
            APPGReport report4;
            report1.setValue(dbs);
            report2.setValue(dbs);
            report3.setValue(dbs);
            report4.setValue(dbs);

            QPixmap pixmap1=report1.grab(report1.rect()); //抓取界面widget区域，可以抓取任意控件区域
            QPixmap pixmap2=report2.grab(report2.rect()); //抓取界面widget区域，可以抓取任意控件区域
            QPixmap pixmap3=report3.grab(report3.rect()); //抓取界面widget区域，可以抓取任意控件区域
            QPixmap pixmap4=report4.grab(report4.rect()); //抓取界面widget区域，可以抓取任意控件区域
            QPainter painter;
            painter.begin(&printer);
            painter.drawPixmap(0,0,pixmap1); //按照坐标画图
            printer.newPage();
            painter.drawPixmap(0,0,pixmap2); //按照坐标画图
            printer.newPage();
            painter.drawPixmap(0,0,pixmap3); //按照坐标画图
            printer.newPage();
            painter.drawPixmap(0,0,pixmap4); //按照坐标画图
            painter.end();

            painter.begin(&printer2);
            painter.drawPixmap(0,0,pixmap1); //按照坐标画图
            printer2.newPage();
            painter.drawPixmap(0,0,pixmap2); //按照坐标画图
            printer2.newPage();
            painter.drawPixmap(0,0,pixmap3); //按照坐标画图
            printer2.newPage();
            painter.drawPixmap(0,0,pixmap4); //按照坐标画图
            painter.end();
            myHelper::ShowMessageBoxInfo("导出完毕！");

        }else if(printType=="输出png"){
            //普瑞森体检系统版本
            qDebug()<<"on_bt_Print_clicked"<<"输出png";
            QString ParentFolder="D:\\data\\"+dbs.UserID;
            QDir dir(ParentFolder);
            QString fileName1,fileName2,fileName3,fileName4;
            if(dir.exists())
            {
                fileName1=ParentFolder+"_1.jpg";
                fileName2=ParentFolder+"_2.jpg";
                fileName3=ParentFolder+"_3.jpg";
                fileName4=ParentFolder+"_4.jpg";
            }else
            {
                bool ok = dir.mkpath(ParentFolder);//创建多级目录
                if(ok)
                {
                    fileName1=ParentFolder+"_1.jpg";
                    fileName2=ParentFolder+"_2.jpg";
                    fileName3=ParentFolder+"_3.jpg";
                    fileName4=ParentFolder+"_4.jpg";
                }
            }

            Login report1;
            HRV report2;
            Anf report3;
            APPGReport report4;
            report1.setValue(dbs);
            report2.setValue(dbs);
            report3.setValue(dbs);
            report4.setValue(dbs);

            QPixmap pixmap1=report1.grab(report1.rect()); //抓取界面widget区域，可以抓取任意控件区域
            QPixmap pixmap2=report2.grab(report2.rect()); //抓取界面widget区域，可以抓取任意控件区域
            QPixmap pixmap3=report3.grab(report3.rect()); //抓取界面widget区域，可以抓取任意控件区域
            QPixmap pixmap4=report4.grab(report4.rect()); //抓取界面widget区域，可以抓取任意控件区域
            pixmap1.save(fileName1);
            pixmap2.save(fileName2);
            pixmap3.save(fileName3);
            pixmap4.save(fileName4);
//            QPainter painter;
//            painter.begin(&printer);
//            painter.drawPixmap(0,0,pixmap1); //按照坐标画图
//            printer.newPage();
//            painter.drawPixmap(0,0,pixmap2); //按照坐标画图
//            printer.newPage();
//            painter.drawPixmap(0,0,pixmap3); //按照坐标画图
//            printer.newPage();
//            painter.drawPixmap(0,0,pixmap4); //按照坐标画图
//            painter.end();
            myHelper::ShowMessageBoxInfo("导出完毕！");
        }

    }

}


void MeasureResultPage::on_pushButton_Vascular_clicked()
{
    ui->tabWidget->setCurrentIndex(4);
}

